//
//  HomeTableVC.m
//  YaoHeAPP
//
//  Created by mac on 15/11/27.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "HomeTableVC.h"
#import "UIImageView+WebCache.h"
#import "WebVC.h"
#import "Masonry.h"
#import "HeadClass.h"
@interface HomeTableVC ()
{
    UIImageView*imageViewHot;
    UIView*viewHot;
    UILabel*labelHot;
    
}
@end

@implementation HomeTableVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"HomeTableVC"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"HomeTableVC"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    tableView.scrollEnabled=NO;
    
        //热门头条
        if (self.isImage==NO)
        {
            tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
            
            
            if (indexPath.row==0)
            {
                
                UITableViewCell*cellfrist=[tableView dequeueReusableCellWithIdentifier:@"Cellfrist"];
                
                if (!cellfrist)
                {
                    
                    cellfrist=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cellfrist"];
                    
                    imageViewHot=[[UIImageView alloc]init];
                    imageViewHot.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width/3+6);
                    imageViewHot.contentMode = UIViewContentModeScaleAspectFill;
                    imageViewHot.clipsToBounds= YES;
                    imageViewHot.tag=1000;
                    [cellfrist addSubview:imageViewHot];
                    
                    //视图View
                    viewHot=[[UIView alloc]init];
                    viewHot.backgroundColor=[UIColor blackColor];
                    viewHot.alpha=0.7;
                    viewHot.frame=CGRectMake(0, self.view.frame.size.width/3+6-30, self.view.frame.size.width, 30);
                    [cellfrist addSubview:viewHot];
                    
                    //标签
                    labelHot=[[UILabel alloc]init];
                    //                label.text=_dataArray[indexPath.row][@"ARTICLE_TITLE"];
                    labelHot.font=[UIFont systemFontOfSize:15];
                    labelHot.backgroundColor=[UIColor clearColor];
                    labelHot.textAlignment=NSTextAlignmentLeft;
                    labelHot.textColor=[UIColor whiteColor];
                    labelHot.frame=CGRectMake(0, self.view.frame.size.width/3+6-25, self.view.frame.size.width, 25);
                    labelHot.tag=2000;
                    [cellfrist addSubview:labelHot];
                    

                    
                }
                
                if (indexPath.row==0){
                    
                    [imageViewHot sd_setImageWithURL:[NSURL URLWithString:_dataArray[0][@"ARTICLE_IMG"]]];
                    
                    labelHot.text=_dataArray[indexPath.row][@"ARTICLE_TITLE"];
                    
                }
                
                return cellfrist;
                
            }else{
                
                UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
                
                if (!cell) {
                    
                    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
                    
                }
                
                cell.textLabel.font=[UIFont systemFontOfSize:13];
                cell.textLabel.text=_dataArray[indexPath.row][@"ARTICLE_TITLE"];
                
                return cell;
                
            }
        
        }
    
   tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    //精彩推荐
    UITableViewCell*imageCell=[tableView dequeueReusableCellWithIdentifier:@"imageCell"];
    
    if (!imageCell)
    {
        imageCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"imageCell"];
        
        UIImageView*imageView=[[UIImageView alloc]init];
        imageView.tag=100;
        imageView.contentMode =UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds= YES;
        imageView.frame=CGRectMake(1, 3, self.view.frame.size.width-2, self.view.frame.size.width/3) ;
        [imageCell addSubview:imageView];
        //剩余时间
        UILabel*dayLabel=[[UILabel alloc]init];
        dayLabel.backgroundColor=[UIColor colorWithRed:45.f/255.f green:66.f/255.f blue:68.f/255.f alpha:0.9];
        dayLabel.layer.masksToBounds=YES;
        dayLabel.layer.cornerRadius=10;
        dayLabel.clipsToBounds=YES;
        dayLabel.textColor=[UIColor whiteColor];
        dayLabel.font=[UIFont systemFontOfSize:13];
        //dayLabel.textAlignment=NSTextAlignmentRight;
        dayLabel.frame=CGRectMake(self.view.frame.size.width-75, 3, 85, 20);
        dayLabel.tag=200;
        [imageCell addSubview:dayLabel];
        UIImageView*timeImg=[[UIImageView alloc]init];
        timeImg.image=[UIImage imageNamed:@"ic_recommend_time"];
        timeImg.frame=CGRectMake(3, 3, 14, 14);
        [dayLabel addSubview:timeImg];
        
        
        
        //已结束的标记
        UIButton*endBtn=[[UIButton alloc]init];
        endBtn.titleLabel.font=[UIFont systemFontOfSize:13];
        [endBtn setTitle:@"已结束" forState:UIControlStateNormal];
        [endBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        endBtn.tag=300;
        [endBtn setBackgroundImage:[UIImage imageNamed:@"bg_recommend_end"] forState:UIControlStateNormal];
        [imageCell addSubview:endBtn];
        [endBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(imageCell.mas_centerX).offset(0);
            make.centerY.equalTo(imageCell.mas_centerY).offset(0);
            make.width.offset(60);   make.height.offset(60);
        }];
    }
    HeadClass*headClass=_dataArray[indexPath.row];
    UIImageView*imageData=(UIImageView*)[imageCell viewWithTag:100];
    [imageData sd_setImageWithURL:[NSURL URLWithString:headClass.Img]];
    UILabel*dayLabel=(UILabel*)[imageCell viewWithTag:200];
    UIButton*endBtn=(UIButton*)[imageCell viewWithTag:300];
    if ([headClass.isShow isEqual:@"是"]) {
        if ([headClass.endDate integerValue]>=0) {
            endBtn.hidden=YES;
            NSInteger day=[headClass.endDate integerValue]+1;
            dayLabel.text=[NSString stringWithFormat:@"      剩:%ld天",(long)day];
        }
        else{
            dayLabel.hidden=YES;
        }
    }
    else{
        dayLabel.hidden=YES;
        endBtn.hidden=YES;
    }
    

    return imageCell;
}
//返回单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isImage==NO)
    {
        if (indexPath.row==0)
        {
            return self.view.frame.size.width/3+6;
        }
        return 46;
    }
    return self.view.frame.size.width/3+6;
}


//选中单元格执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.isImage==NO)
    {
        [MobClick event:@"news"];
        
        WebVC*webvc=[WebVC new];
        
        NSString*str=_dataArray[indexPath.row][@"ARTICLE_LINK"];
        if ([str hasPrefix:@"http"])
        {
            webvc.titleTxt=@"热门头条";
            webvc.urlString=_dataArray[indexPath.row][@"ARTICLE_LINK"];
            [self.navigationController pushViewController:webvc animated:YES];
        }
        else{
           return;
        }
    }
    else
    {
        [MobClick event:@"recommendedActivities"];
        
        WebVC*webvc=[WebVC new];
        webvc.titleTxt=@"精彩推荐";
        HeadClass*headClass=_dataArray[indexPath.row];
        NSString*str=headClass.link;
        if ([str hasPrefix:@"http"])
        {
            webvc.urlString=headClass.link;
            [self.navigationController pushViewController:webvc animated:YES];
        }
        else{
            return;
        }
    }
}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

@end
